package com.my.ads.lib.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.myadlib.R;
import com.my.ads.lib.AdSummary;

import java.io.File;

/**
 * Created by xuanyang on 2018/3/6.
 */

public class MediaActivity extends BaseActivity {
    private ImageView mediaView, cancel, appWall;
    private RelativeLayout adView;
    private String filePath = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media);
        mediaView = (ImageView) findViewById(R.id.image_activity);
        cancel = (ImageView) findViewById(R.id.cancel);
        appWall = (ImageView) findViewById(R.id.appwall);
        adView = (RelativeLayout) findViewById(R.id.ad_jianting);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AdSummary.getInstance().showInterstitialAd(MediaActivity.this, "");
                finish();
                Intent startMain = new Intent(Intent.ACTION_MAIN);
                startMain.addCategory(Intent.CATEGORY_HOME);
                startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(startMain);
            }
        });

        appWall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AdSummary.getInstance().showInterstitialAd(MediaActivity.this, "");
                Intent in = new Intent(MediaActivity.this, GiftActivity.class);
                startActivity(in);
            }
        });

        try {
            // 没有刷新广告
            AdSummary.getInstance().showNativeAd(this, adView, false);
        } catch (Exception e) {
            e.printStackTrace();
        }


        Intent intent = getIntent();
        // 0 图片 1视频


        filePath = intent.getStringExtra("url");
        int type = intent.getIntExtra("type", 0);

        if (!"".equals(filePath)) {
            if (type == 0) {
                File file = new File(filePath);
                if (file.exists()) {
                    Bitmap bm = BitmapFactory.decodeFile(filePath);
                    //将图片显示到ImageView中
                    mediaView.setImageBitmap(bm);
                }
            } else {
                mediaView.setImageBitmap(getVideoThumbnail(filePath, 60, 60,
                        MediaStore.Images.Thumbnails.MINI_KIND));
            }

        }


    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
//        Log.i(Config.TagLog, "adx back begin");
        AdSummary.getInstance().showInterstitialAd(this, "");

        finish();
        // 返回桌面 （目前返回应用首页会更好一点）
//        Intent startMain = new Intent(Intent.ACTION_MAIN);
//        startMain.addCategory(Intent.CATEGORY_HOME);
//        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        startActivityNewTask(startMain);

    }


    private Bitmap getVideoThumbnail(String videoPath, int width, int height,
                                     int kind) {
        Bitmap bitmap = null;
        // 获取视频的缩略图
        bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
//        System.out.println("w"+bitmap.getWidth());
//        System.out.println("h"+bitmap.getHeight());
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
                ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        return bitmap;
    }


}
